using UnityEngine;
using Prota;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering;
using Prota.Unity;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Prota.Unity
{
    
    public class ProtaCustomRenderPassFeature : ScriptableRendererFeature
    {
        public RenderPassEvent renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
        
        [SerializeField] ProtaRenderPass[] passes = Array.Empty<ProtaRenderPass>();
        
        public IEnumerable<ProtaRenderPass> validPasses => passes.Where(x => x != null).ToArray();
        
        [NonSerialized] public ProtaUniqueRenderPass pass;
        
        public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
        {
            pass = new ProtaUniqueRenderPass(this);
            renderer.EnqueuePass(pass);
        }

        public override void SetupRenderPasses(ScriptableRenderer renderer, in RenderingData renderingData)
        {
            pass.SetTarget(renderer.cameraColorTargetHandle, renderer.cameraDepthTargetHandle);
        }
        
        public override void Create()
        {
            foreach (var pass in validPasses)
            {
                pass.OnCreate();
            }
        }
        
        protected override void Dispose(bool disposing)
        {
            foreach (var pass in validPasses)
            {
                pass.RendererCleanUp();
            }
            
            base.Dispose(disposing);
        }
    }
    
    public class ProtaUniqueRenderPass : ScriptableRenderPass
    {
        ProtaCustomRenderPassFeature config;
        
        
        public ProtaUniqueRenderPass(ProtaCustomRenderPassFeature x)
        {
            config = x;
            this.renderPassEvent = x.renderPassEvent;
        }
        
        public void SetTarget(RTHandle targetHandle, RTHandle depthHandle)
        {
            foreach (var pass in config.validPasses)
            {
                pass.cameraColorTexture = targetHandle;
                pass.cameraDepthTexture = depthHandle;
            }
        }

        
        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            foreach (var pass in config.validPasses)
            {
                pass.rendererName = config.name;
                pass.OnRender(context, ref renderingData);
            }
        }

        public override void FrameCleanup(CommandBuffer cmd)
        {
            foreach (var pass in config.validPasses)
            {
                pass.FrameCleanUp();
            }
            base.FrameCleanup(cmd);
        }
        
        // ====================================================================================================
        // ====================================================================================================
        
        
        
    }
}
